<link href="<?php echo base_url('theme/css/frontend/account-manager.css'); ?>" rel="stylesheet">
<link rel="stylesheet" href="<?php echo base_url('theme/css/frontend/jquery.Jcrop.min.css'); ?>" type="text/css" />
<div class="account-wrapper">
  <?php echo validation_errors('<div class="alert alert-error">','</div>'); ?>
  <h3>Đăng ký rao vặt</h3>
  <hr class="hr" />  
   
  <form method="post" class="form-horizontal account-container" action="">
    <h5>Thông tin cá nhân</h5>
    <hr class="hr" />
    <div class="form-wrapper">
      <div class="control-group">
        <label class="control-label">Ảnh đại diện</label>
        <div class="controls">
        <?php if(empty($user['avatar'])): ?>
          <img style="width:100px; height:110px;" class="img-polaroid" src="<?php echo base_url('theme/images/frontend/user-default-avatar.jpg'); ?>">
        <?php else: ?>
          <img style="width:100px; height:110px;" class="img-polaroid" src="<?php echo base_url('upload/user/' . $user['user_id'] . '/' . $user['avatar']); ?>">
        <?php endif; ?>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label">Họ và tên</label>
        <div class="controls"><?php echo $user['real_name']; ?></div>
      </div>
      <div class="control-group">
        <label class="control-label">Ngày sinh</label>
        <div class="controls">
          <?php if ($num = strtotime($user['birthday'])) {
            echo date('d/m/Y', $num);
          } else {
            echo '--';
          } ?>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label">Giới tính</label>
        <div class="controls"><?php echo getSexType($user['sex']); ?></div>
      </div>
      <div class="control-group">
        <label class="control-label">Địa chỉ</label>
        <div class="controls"><?php echo $user['address']; ?></div>
      </div>
      <div class="control-group">
        <label class="control-label">Email</label>
        <div class="controls"><?php echo $user['email']; ?></div>
      </div>
      <div class="control-group">
        <label class="control-label">Điện thoại</label>
        <div class="controls"><?php echo $user['tel']; ?></div>
      </div>
      <div class="control-group">
		  <a class="pull-right" href="<?php echo base_url('user/changingAccountInformation'); ?>">Chỉnh sửa thông tin cá nhân</a>
      </div>
    </div>
  </form> 
  
  <form method="post" id="ad-form" class="form-horizontal account-container" action=<?php echo base_url('ad/add'); ?>>
    <h5>Thông tin sản phẩm</h5>
    <hr class="hr" />
    <div class="form-wrapper">
      <div class="control-group">
       	<label for="category_select" class="control-label">Chuyên mục</label>
       	<div class="controls">
       	  <a href="<?php echo base_url('ad/categories?ajax=true'); ?>" rel=categoryPopup[ajax]>
            <input type=text id=category_select class="input-xxlarge" value="<?php echo set_value('category_id', getAdCategories($ad['category_id'])); ?>">	
            <input type=hidden name=category_id id=category_id value="<?php echo set_value('category_id', $ad['category_id']); ?>">             	
            <input type="hidden" name=slug id="slug" value="<?php echo set_value('slug', $ad['slug']); ?>"/>
          </a>
          <script type="text/javascript">
          $(document).ready(function(){              
            // Category popup
            $("a[rel^='categoryPopup']").prettyPhoto({
              ajaxcallback: function() {
  	            // category popup
  	            $(".category-list h4 a").click(function(){
  	  	          var category_id = $(this).data('category'); 
  	              $('#category_select').val($(this).text()); 
  	              $('#category_id').val(category_id);  	

  	              $('#slug').val($(this).html());
  	                            
  	              $.prettyPhoto.close();
  	          	  return false;
  	            }); 

  	            $(".category-list > .column > ul > li > a").click(function(){
  	  	          var category_id = $(this).data('category'); 
  	              $('#category_select').val($(this).text()); 
  	              $('#category_id').val(category_id);  	 

  	              var root = $(this).parent().parent().prev().children('a').html() + '/';
  	              $('#slug').val(root + $(this).html());
  	                           
  	              $.prettyPhoto.close();
  	          	  return false;
  	            });

  	            $(".category-list > .column > ul > li > ul > li > a").click(function(){
  	  	          var category_id = $(this).data('category'); 
  	              $('#category_select').val($(this).text()); 
  	              $('#category_id').val(category_id);  	 

  	              var parent = $(this).parent().parent().prev(); // a
  	              var root = parent.parent().parent().prev().children('a').html() + '/';
  	              
  	              $('#slug').val(root + parent.html() + '/' + $(this).html());
  	                           
  	              $.prettyPhoto.close();
  	          	  return false;
  	            });
              },
              markup: '<div class="pp_container" role="dialog"><div class="pp_loaderIcon"></div><div class="pp_placeholder"></div></div><div class="pp_overlay"></div>'
            });
          });
          </script>
        </div>
      </div>
      <div class="control-group">
       	<label for="district" class="control-label">Tỉnh thành</label>
       	<div class="controls">
       	  <a href="<?php echo base_url('ad/cities?ajax=true'); ?>" rel=postAreaPopup[ajax]>
            <input type=text id=district name=district class="input-xxlarge" value="<?php echo set_value('district', $ad['district']); ?>">	
            <input type=hidden name=city id=city value="<?php echo set_value('city', $ad['city']); ?>">             	
          </a>
          <script type="text/javascript">
          $(document).ready(function(){
            // City popup
            $("a[rel^='postAreaPopup']").prettyPhoto({
              type: 'city',
              ajaxcallback: function() {
            	// city popup
  	            $(".city-list .column > ul > li > a").click(function(){ 
  	              $('#district').val($(this).text()); 
  	              $('#city').val($(this).data('city'));
  	              $.prettyPhoto.close();
  	          	  return false;
  	            }); 

  	            $(".city-list .column > ul > li > ul > li > a").click(function(){ 
  	              $('#district').val($(this).text()); 
  	              $('#city').val($(this).parent().parent().prev().data('city'));
  	              $.prettyPhoto.close();
  	          	  return false;
  	            });
              },
              markup: '<div class="pp_container" role="dialog"><div class="pp_loaderIcon"></div><div class="pp_placeholder"></div></div><div class="pp_overlay"></div>',
              height: 560,
              width: 880
            });
          });
          </script>
        </div>
      </div>
      <div class="control-group">
        <label for="title" class="control-label">Tiêu đề rao vặt</label>
        <div class="controls">
          <input type=text name=title id=title class="input-xxlarge" value="<?php echo set_value('title', $ad['title']); ?>">
       	</div>
      </div>
      <div class="control-group">
        <label for="price" class="control-label">Giá bán</label>
        <div class="controls">
          <input type=text name=price id=price class="input-xxlarge" value="<?php echo set_value('price', $ad['price']); ?>">
       	</div>
      </div>
      <div class="control-group">
        <label for="base_price" class="control-label">Giá gốc</label>
        <div class="controls">
          <input type=text name=base_price id=base_price class="input-xxlarge" value="<?php echo set_value('base_price', $ad['base_price']); ?>">
       	</div>
      </div>
      <div class="control-group">
        <label for="date_edit" class="control-label">Cập nhật ngày</label>
        <div class="controls">
          <input type=text name=date_edit id=date_edit class="input-xxlarge" value="<?php echo set_value('date_edit', $ad['date_edit']); ?>">
       	</div>
      </div>
      <div class="control-group">
        <label for="warranty" class="control-label">Bảo hành</label>
        <div class="controls">
          <input type=text name=warranty id=warranty class="input-xxlarge" value="<?php echo set_value('warranty', $ad['warranty']); ?>">
       	</div>
      </div>
      <div class="control-group">
        <label for="status" class="control-label">Tình trạng</label>
        <div class="controls">
          <input type=text name=status id=status class="input-xxlarge" value="<?php echo set_value('status', $ad['status']); ?>">
       	</div>
      </div>
      <div class="control-group">
        <label for="forms" class="control-label">Hình thức</label>
        <div class="controls">
          <input type=text name=forms id=forms class="input-xxlarge" value="<?php echo set_value('forms', $ad['forms']); ?>">
       	</div>
      </div>
      <div class="control-group">
        <label for="made_in" class="control-label">Xuất sứ</label>
        <div class="controls">
          <input type=text name=made_in id=made_in class="input-xxlarge" value="<?php echo set_value('made_in', $ad['made_in']); ?>">
       	</div>
      </div>
      <div class="control-group">
        <label for="shipping" class="control-label">Vận chuyển</label>
        <div class="controls">
          <input type=text name=shipping id=shipping class="input-xxlarge" value="<?php echo set_value('shipping', $ad['shipping']); ?>">
       	</div>
      </div>
      <div class="control-group">
        <label for="quantity" class="control-label">Số lượng hàng</label>
        <div class="controls">
          <input type=text name=quantity id=quantity class="input-xxlarge" value="<?php echo set_value('quantity', $ad['quantity']); ?>">
       	</div>
      </div>
      <div class="control-group">
        <label for="description" class="control-label">Miêu tả</label>
       	<div class="controls">
          <textarea name="description" id="description"><?php echo set_value('description', $ad['description']); ?></textarea>
          <!-- editor -->
          <script src="<?php echo base_url('theme/third_party/epicEditor/scripts/epiceditor.js'); ?>" type="text/javascript"></script>
          <script type="text/javascript">
          //<![CDATA[    
          epicEditor.init({
          	containId: 'description',
          	buttons1 : 'bold,italic,underline,strikethrough,fontsize,fontname,justifyleft,justifycenter,justifyright,justifyfull,removeformat,inserthtml',
          	width:502,
          	height:200
          });
          //]]>
          </script>
          <!-- /editor -->
        </div>
      </div>
      <div class="control-group">
        <label for="intro_video" class="control-label">Video Youtube</label>
       	<div class="controls">
       	  <input type=text name=intro_video id=intro_video class="input-xxlarge" value="<?php echo set_value('intro_video', $ad['intro_video']); ?>">
        </div>
      </div>
      <div class="control-group">
        <div class=controls>
          <button class="btn btn-primary" type=submit name=ad-submit value="Lưu thay đổi">Lưu thay đổi</button>
          <button class="btn" type="button">Hủy bỏ</button>
        </div>
      </div>
    </div>
  </form>
  
    <?php $types = array('logo' => 'Logo', 
  					     'avatar' => 'Ảnh đại diện'); ?>
  <?php foreach ($types as $type => $name): ?>
  <div style="border-left: 1px solid #C6C6C6; border-right: 1px solid #C6C6C6; padding: 15px 30px;">
    <h5><?php echo $name ?></h5>
    <hr class="hr hr2" />
    <div class="<?php echo $type ?>">
      <form method="post" id="upload_<?php echo $type ?>" class="form-horizontal" action=<?php echo base_url('ad/uploadFile/' . $type); ?> enctype=multipart/form-data > 
        <div class=control-group>
          <label for="<?php echo $type ?>" class="control-label">Chọn ảnh<i>*</i></label>
         	<div class="controls">
         	  <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="600000" />
         	  <input type="file" id="file_<?php echo $type ?>" name="file_<?php echo $type ?>[]"/>
         	</div>
        </div>
        <div class="submitbutton">
          <button type="submit">Upload Files</button>
        </div>
      </form>
      <ul class="thumbnails">
      <?php
        // made dir
        $dir = 'upload/ad/' . $type . '/' . $_SESSION['userLoggedIn']['id'];
        if (!file_exists($dir)) {
            @mkdir($dir, 0777, true);
        }
        // Open the folder
        $dir_handle = opendir($dir);    
        // Loop through the files
        while ($file = readdir($dir_handle)) {
          if($file == "." || $file == ".." || $file == "index.php" )
            continue;
          echo '<li style="height:60px; padding-left: 5px;"><img width="90" src="' . base_url('upload/ad/' . $type . '/' . $_SESSION['userLoggedIn']['id'] . '/' . $file) .'" /><div>'. $file .'</div></li>';
        }
        // Close
        closedir($dir_handle);
      ?>
      </ul>
      <div id="<?php echo $type; ?>_append"></div>
      <form class="form-horizontal" action="<?php echo base_url('ad/addFile/' . $type); ?>" method="post">
        <div class="control-group">
          <div class=controls>
            <input type=hidden name=ad_id id=ad_id value="<?php echo $ad['ad_id'] ?>"/>
            <input type="hidden" id="data<?php echo $type ?>CropJson" name="data<?php echo $type ?>CropJson" />
            <button class="btn" type=submit value="Lưu thay đổi">Lưu thay đổi</button>
            <button class="btn" type="button">Hủy bỏ</button>
          </div>
        </div>
      </form>
    </div>
  </div>
  <?php endforeach; ?>
  
  <div style="border-left: 1px solid #C6C6C6; border-right: 1px solid #C6C6C6; padding: 15px 30px;">
    <h5>Album doanh nghiệp</h5>
    <hr class="hr hr2" />
    <div class=album>
      <form method="post" id="upload_album" class="form-horizontal" action=<?php echo base_url('ad/uploadFile/album'); ?> enctype=multipart/form-data > 
        <div class=control-group>
          <label for="email" class="control-label">Chọn ảnh<i>*</i></label>
         	<div class="controls">
         	  <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="600000" />
         	  <input type="file" id="file_album" name="file_album[]" multiple="multiple" />
         	</div>
        </div>
        <div class="submitbutton">
          <button type="submit">Upload Files</button>
        </div>
      </form>
      <ul class="thumbnails">
      <?php
        // made dir
        $albumDir = 'upload/ad/album/' . $_SESSION['userLoggedIn']['id'];
        if (!file_exists($albumDir)) {
            @mkdir($albumDir, 0777, true);
        }
        // Open the folder
        $dir_handle = @opendir($albumDir) or die("Unable to open $albumDir");    
        // Loop through the files
        while ($file = readdir($dir_handle)) {
          if($file == "." || $file == ".." || $file == "index.php" )
            continue;
          echo '<li style="height:60px; padding-left: 5px;"><img width="90" src="' . base_url('upload/ad/album/' . $_SESSION['userLoggedIn']['id'] . '/' . $file) .'" /></li>';
        }
        // Close
        closedir($dir_handle);
      ?>
      </ul>
      <div id="album_append"></div>
      <form class="form-horizontal" action="<?php echo base_url('ad/addFile/album'); ?>" method="post">
        <div class="control-group">
          <div class=controls>
            <input type=hidden name=ad_id id=ad_id value="<?php echo $ad['ad_id'] ?>"/>
            <input type="hidden" id="dataAlbumCropJson" name="dataAlbumCropJson" />
            <button class="btn" type=submit value="Lưu thay đổi">Lưu thay đổi</button>
            <button class="btn" type="button">Hủy bỏ</button>
          </div>
        </div>
      </form>
      <script src="<?php echo base_url('theme/js/frontend/jquery.Jcrop.min.js'); ?>"></script>
      <script src="<?php echo base_url('theme/js/frontend/jquery.json-2.4.min.js'); ?>"></script>
      <script src="<?php echo base_url('theme/js/frontend/ad-filedrag.js'); ?>"></script>
    </div>
  </div>
  
  <form method="post" class="form-horizontal account-container" action=<?php echo base_url('ad/add'); ?>>
    <script src="<?php echo base_url('theme/js/frontend/google.map.js'); ?>" type="text/javascript"></script>
	<script type="text/javascript">
    $(document).ready(function(){
      function OnLoad() {
        // Initialize the map with default UI.
        gMap = new google.maps.Map(document.getElementById("map"), {
          center: new google.maps.LatLng(21.0327, 105.8489),	// Ha Noi
          zoom: 13,
          mapTypeId: 'roadmap'
        });
      
        // Initialize the local searcher
        gLocalSearch = new GlocalSearch();
        gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);

        var edit = "<?php echo $ad['lat']; ?>";
        if (edit) {
  		  // Khởi tạo marker khi edit
          marker = new google.maps.Marker({
            position: new google.maps.LatLng(document.getElementById('lat').value, document.getElementById('lng').value),
            map: gMap,
        	  draggable: true
          });
          
          google.maps.event.addListener(marker, 'dragend', function() {
        	  document.getElementById('lat').value = this.get('position').lat();
        	  document.getElementById('lng').value = this.get('position').lng();
          }); 

          gMap.setCenter(new google.maps.LatLng(document.getElementById('lat').value, document.getElementById('lng').value));
        }
      }

        GSearch.setOnLoadCallback(OnLoad);
      });
    </script>
    <h5>Vị trí người đăng tin</h5>
    <hr class="hr" />
    <div class="form-wrapper">
      <div class="control-group">
        <label for="record" class="control-label">Bản đồ google</label>
        <div class="controls">
		  <input type="text" id="queryInput" value="" style="width: 434px;" />
          <input type="button" class="btn btn-primary" value="Go!" onclick="doSearch()"/>
       	</div>
      </div>
      <div class="control-group">
        <div class="controls">
		  <div id="map" style="height: 350px; width: 500px; border: 1px solid #979797;"></div>
       	</div>
      </div>
      <div class="control-group">
        <div class=controls>
		  <input type="hidden" name=lat id="lat" value="<?php echo set_value('lat', $ad['lat']); ?>"/>
		  <input type="hidden" name=lng id="lng" value="<?php echo set_value('lng', $ad['lng']); ?>"/>
          <input type=hidden name=ad_id id=ad_id value="<?php echo $ad['ad_id'] ?>"/>
          <button class="btn" type="submit" name=map value="Lưu thay đổi">Lưu thay đổi</button>
          <button class="btn" type="button">Hủy bỏ</button>
        </div>
      </div>
    </div>
  </form>
</div>

<script type="text/javascript">
    $(document).ready(function() {
      // validate form on keyup and submit
      $("#ad-form").validate({
    	ignore: "",
  	    submitHandler: function(form) {
  	      $pnk('description').value = $pnk('epic_iframe').contentWindow.document.body.innerHTML;
  		  form.submit();
  		}
      }); // end validate
      
    });
  </script>  